package com.qf.dao.impl;

import com.qf.dao.AccountDao;
import com.qf.pojo.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @Description: 公用常量
 * @Data: 2022/9/1 22:06
 * @Pacakge: com.qf.dao.impl
 * @ClassName: AccountDaoImpl
 * @Author: fan
 */
public class AccountDaoImpl implements AccountDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 根据用户名查询账号
     * @param name 用户名
     * @return
     */
    @Override
    public Account findAccountByName(String name) {
        List<Account> list = jdbcTemplate.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), name);
        if (list == null || list.size() == 0) {
            return null;
        }
        if (list.size() > 1) {
            throw new RuntimeException();
        }
        return list.get(0);
    }

    /**
     * 修改账户信息
     * @param account 账户
     */
    @Override
    public void updateAccount(Account account) {
        jdbcTemplate.update("update account set money = ? where name = ?",account.getMoney(),account.getName());
    }
}
